package com.yst.sqlbuilder;

import com.yst.lambda.TableFieldFunc;
import lombok.Getter;

import java.util.Objects;

@Getter
public class SQLFieldCompareExp<T> extends SQLField<T>{
    private Symbol symbol;

    public SQLFieldCompareExp<T> setSymbol(Symbol symbol){
        this.symbol=symbol;
        return this;
    }

    private Object value;

    public SQLFieldCompareExp<T> setValue(Object value){
        this.value=value;
        return this;
    }

    public SQLFieldCompareExp(IAliasExpression tableAlias,TableFieldFunc<T,?> field){
        super(tableAlias,field);
    }

    @Override
    public String generateExpression(GenerateParam param) {

        if (Objects.isNull(value)){
            return "";
        }

        if (value instanceof IExpression) {
            return getRealField().concat(symbol.symbol).concat(((IExpression)value).generateExpression(param));
        }
        if (value instanceof Number||value instanceof Boolean){
            return getRealField().concat(symbol.symbol).concat(value.toString());
        }

        String strValue=value.toString();
        SQLSafe.getInstance().checkValue(strValue);

        return getRealField().concat(symbol.symbol).concat(String.format("'%s'",strValue));
    }


}
